<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="材料名称" prop="materialName">
                <el-input v-model="queryParams.materialName" placeholder="请输入材料名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                <el-button type="success" size="small" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['decor:material:add']">新增</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="materialList" row-key="materialId" default-expand-all :tree-props="{ children: 'updateRecords' }">
            <el-table-column label="材料ID" :show-overflow-tooltip="true" align="center" prop="materialId" />
            <el-table-column label="材料商" :show-overflow-tooltip="true" align="center"
                prop="materialSupplierName"></el-table-column>
            <el-table-column label="材料名称" :show-overflow-tooltip="true" align="center"
                prop="materialName"></el-table-column>
            <el-table-column label="材料简述" :show-overflow-tooltip="true" align="center"
                prop="materialBriefly"></el-table-column>
            <el-table-column label="点赞数" width="80" align="center" prop="likes"> </el-table-column>
            <el-table-column label="访问量" width="80" align="center" prop="visits"> </el-table-column>
            <el-table-column label="材料状态" align="center" prop="materialStatus">
                <template #default="scope">
                    <dict-tag :options="material_status" :value="scope.row.materialStatus" />
                </template>
            </el-table-column>
            <el-table-column label="排序" :show-overflow-tooltip="true" align="center" prop="sort1">
                <template #default="scope">
                    <div>{{ scope.row.sort1 }}</div>
                    <el-button link type="warning" size="small" @click="editSort(scope.row)"
                        v-hasPermi="['decor:material:edit']">修改排序</el-button>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" size="small" @click="handleUpdate(scope.row)"
                        v-hasPermi="['decor:material:edit']">修改</el-button>
                    <el-button link type="warning" icon="Delete" size="small" @click="handleDelete(scope.row)"
                        v-hasPermi="['decor:material:remove']">删除</el-button>
                    <el-button link type="primary" v-show="scope.row.materialStatus === 1 || scope.row.materialStatus === 3"
                        :icon="scope.row.materialStatus === 3 ? 'Upload' : 'Download'" size="small"
                        @click="handleDownload(scope.row)" v-hasPermi="['decor:material:edit']">{{ scope.row.materialStatus
                            === 3 ?
                            '上架' : '下架' }}</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
</template>

<script setup>
import { listMaterial, delMaterial, updateMaterial } from "@/api/decor/materialAudit";
const router = useRouter();
const { proxy } = getCurrentInstance();
const { material_status } = proxy.useDict('material_status');
// 表格数据
const materialList = ref([]);
// 遮罩层
const loading = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialName: null,
        // orderByColumn: 'createTime',
        // isAsc: 'desc'
    },
});

const { queryParams } = toRefs(data);

/** 查询材料列表 */
function getList() {
    loading.value = true;
    listMaterial(queryParams.value).then(response => {
        materialList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
    router.push('/materialAuditting/editMaterialAuditting')
}

/** 修改按钮操作 */
function handleUpdate(row) {
    router.push('/materialAuditting/editMaterialAuditting/' + row.materialId)
}
// 调整排序
const editSort = (row) => {
    proxy.$prompt('请输入"' + row.materialName + '"的排序', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^(?:[1-9][0-9]{0,3})$/,
        inputErrorMessage: "请输入介于 1 和 9999 之间的数字",
    }).then(({ value }) => {
        const data = {
            ...row,
            sort1: value
        }
        updateMaterial(data).then(response => {
            proxy.$modal.msgSuccess("修改排序成功");
            getList()
        });
    }).catch(() => { });
}
/** 删除按钮操作 */
function handleDelete(row) {
    const _materialIds = row.materialId;
    proxy.$modal.confirm('是否确认删除材料名称为"' + row.materialName + '"的数据？').then(function () {
        return delMaterial(_materialIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}
// 下架操作
const handleDownload = (row) => {
    const status = row.materialStatus === 3 ? '上架' : '下架'
    const materialStatus = row.materialStatus === 3 ? 1 : 3
    proxy.$modal.confirm(`是否确认${status}材料"' ${row.materialName}  '"？`).then(function () {
        return updateMaterial({ ...row, materialStatus: materialStatus });
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess(`${status}成功`);
    }).catch(() => { });
}
getList();
</script>
